package com.lenovo.srl.dil.tiltbase.lib;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.RadioGroup.OnCheckedChangeListener;

import com.lenovo.srl.dil.tiltbase.lib.fragment.BaseFragment.BallInitPosition;
import com.lenovo.srl.dil.tiltbase.lib.fragment.BaseFragment.CtrlMode;
import com.lenovo.srl.dil.tiltbase.lib.utils.PreferenceUtil;

public class SettingsActivity extends FragmentActivity {

	static int mCtrlMode;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		mCtrlMode = getIntent().getIntExtra("mode", CtrlMode.Perfect.ordinal());
		SettingsFragment fragment = new SettingsFragment();
		getSupportFragmentManager().beginTransaction()
				.replace(R.id.container, fragment).commit();
	}

	public static class SettingsFragment extends Fragment implements
			View.OnClickListener {

		EditText tilt_gain, move_up_init_angle, move_down_init_angle,
				over_icon_speed_ratio,over_icon_min_speed_ratio, move_up_down_time_delay;
		Button mSave;

		RadioGroup mPositionRadioGroup;
		RadioButton mRadioButtonLeft, mRadioButtonCenter, mRadioButtonRight;

		int mBallInitPosition;

		@Override
		public void onCreate(Bundle savedInstanceState) {
			super.onCreate(savedInstanceState);
		}

		@Override
		public View onCreateView(LayoutInflater inflater, ViewGroup container,
				Bundle savedInstanceState) {
			final View rootView = inflater.inflate(R.layout.fragment_settings,
					container, false);
			tilt_gain = (EditText) rootView.findViewById(R.id.tilt_gain);
			move_up_init_angle = (EditText) rootView
					.findViewById(R.id.move_up_init_angle);
			move_down_init_angle = (EditText) rootView
					.findViewById(R.id.move_down_init_angle);
			if (mCtrlMode != CtrlMode.PostionVelocity.ordinal()) {
				((View)move_up_init_angle.getParent()).setVisibility(View.GONE);
				((View)move_down_init_angle.getParent()).setVisibility(View.GONE);
			}
			over_icon_speed_ratio = (EditText) rootView
					.findViewById(R.id.over_icon_speed_ratio);
			over_icon_min_speed_ratio= (EditText) rootView
					.findViewById(R.id.over_icon_min_speed_ratio);
			move_up_down_time_delay = (EditText) rootView
					.findViewById(R.id.move_up_down_time_delay);
			if (mCtrlMode == CtrlMode.Position.ordinal()
					|| mCtrlMode == CtrlMode.Velocity.ordinal()) {
				((View)over_icon_speed_ratio.getParent()).setVisibility(View.GONE);
			}
			if (mCtrlMode != CtrlMode.PostionVelocity.ordinal()) {
				((View)move_up_down_time_delay.getParent()).setVisibility(View.GONE);
				((View)over_icon_min_speed_ratio.getParent()).setVisibility(View.GONE);
			}
			mSave = (Button) rootView.findViewById(R.id.btn_save);

			mPositionRadioGroup = (RadioGroup) rootView
					.findViewById(R.id.radioGroup);
			mRadioButtonLeft = (RadioButton) rootView
					.findViewById(R.id.radio_left);
			mRadioButtonCenter = (RadioButton) rootView
					.findViewById(R.id.radio_center);
			mRadioButtonRight = (RadioButton) rootView
					.findViewById(R.id.radio_right);
			((View)mPositionRadioGroup.getParent()).setVisibility(View.GONE);
			init();
			// 绑定一个匿名监听器
			mPositionRadioGroup
					.setOnCheckedChangeListener(new OnCheckedChangeListener() {

						@Override
						public void onCheckedChanged(RadioGroup arg0, int arg1) {
							if (arg1 == R.id.radio_center) {
								mBallInitPosition = BallInitPosition.Center
										.ordinal();
							} else if (arg1 == R.id.radio_left) {
								mBallInitPosition = BallInitPosition.Left
										.ordinal();
							} else if (arg1 == R.id.radio_right) {
								mBallInitPosition = BallInitPosition.Right
										.ordinal();
							}
						}
					});

			return rootView;
		}

		private void init() {
			tilt_gain.setText("" + PreferenceUtil.get_tilt_gain());
			move_up_init_angle.setText(""
					+ PreferenceUtil.get_move_up_init_angle());
			move_down_init_angle.setText(""
					+ PreferenceUtil.get_move_down_init_angle());
			over_icon_speed_ratio.setText(""
					+ PreferenceUtil.get_over_icon_speed_ratio());
			over_icon_min_speed_ratio.setText(""
					+ PreferenceUtil.get_over_icon_min_speed_ratio());
			move_up_down_time_delay.setText(""
					+ PreferenceUtil.get_move_up_down_time_delay());
			mBallInitPosition = PreferenceUtil.get_ball_default_position();
			if (mBallInitPosition == BallInitPosition.Center.ordinal()) {
				mRadioButtonCenter.setChecked(true);
			} else if (mBallInitPosition == BallInitPosition.Left.ordinal()) {
				mRadioButtonLeft.setChecked(true);
			} else {
				mRadioButtonRight.setChecked(true);
			}
			mSave.setOnClickListener(this);
		}

		@Override
		public void onClick(View v) {
			PreferenceUtil.set_tilt_gain(Integer.parseInt(tilt_gain.getText()
					.toString()));
			PreferenceUtil.set_move_up_init_angle(Integer
					.parseInt(move_up_init_angle.getText().toString()));
			PreferenceUtil.set_move_down_init_angle(Integer
					.parseInt(move_down_init_angle.getText().toString()));
			PreferenceUtil.set_over_icon_speed_ratio(Float
					.parseFloat(over_icon_speed_ratio.getText().toString()));
			PreferenceUtil.set_over_icon_min_speed_ratio(Float
					.parseFloat(over_icon_min_speed_ratio.getText().toString()));
			PreferenceUtil.set_move_up_down_time_delay(Integer
					.parseInt(move_up_down_time_delay.getText().toString()));
			PreferenceUtil.set_ball_default_position(mBallInitPosition);
			getActivity().finish();
		}
	}
}
